package com.jky.stmall.manger.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jky.stmall.manger.entity.Goods;
import com.jky.stmall.manger.entity.GoodsPic;
import com.jky.stmall.manger.mapper.GoodsMapper;
import com.jky.stmall.manger.service.GoodsPicService;
import com.jky.stmall.manger.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Service层的实现类
 * 1.需要实现Service层的接口
 * 2.添加Service注解
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    //自动注入Mapper层的对象
    @Resource
    private GoodsMapper goodsMapper;

    //注入GoodsPic的Service层对象
    @Resource
    private GoodsPicService goodsPicService;

    /**
     * 使用Spring事务
     */
  /*  @Transactional(
            isolation = Isolation.SERIALIZABLE,  //ACID  隔离性
            propagation = Propagation.REQUIRED,//事务的传播机制 7个，面试会问
            rollbackFor = Exception.class//回滚,当方法中抛出任何异常时，回滚事务，没抛异常则提交事务
    )*/
    @Transactional(
            isolation = Isolation.SERIALIZABLE,
            propagation = Propagation.REQUIRED,
            rollbackFor = Exception.class
    )

    @Override
    public int add(Goods goods) throws Exception {

        //1.验证参数
        if (goods.getName() == null || goods.getName().length() == 0) {
            throw new Exception("分类名不能为空");
        }
        if (goods.getRecom() == null) {
            throw new Exception("推荐不能为空");
        }
        //2.逻辑处理
        //3.访问数据库

        //保存图片的数据，还要保存商品的图片
        //1.先保存商品，获取数据库自动生成的ID主键
        int code = goodsMapper.insert(goods); //code>0,才能保存图片

        if (code > 0) {
            //2.再保存图片 ,需要用到商品ID
            for (GoodsPic pic : goods.getPics()) {
                pic.setGoodsId(goods.getId());
            }
            goodsPicService.addPatch(goods.getPics());
        }
        return code;
    }

    @Override
    public int remove(Integer id) throws Exception {
        Goods goods = goodsMapper.selectById(id);

        if (goods == null) {
            throw new Exception("ID不存在");
        }
        //只会删除goods，不会删除图片

        return goodsMapper.delete(id);
    }

    @Override
    public int edit(Goods goods) throws Exception {
        Goods sGoods = goodsMapper.selectById(goods.getId());

        if (sGoods == null) {
            throw new Exception("ID不存在");
        }

        //保存图片的数据，还要保存商品的图片
        //1.先保存商品，获取数据库自动生成的ID主键
        int code = goodsMapper.update(goods);
        if (code > 0) {
            //2.再保存图片 ,需要用到商品ID
            for (GoodsPic pic : goods.getPics()) {
                pic.setGoodsId(goods.getId());
            }
            goodsPicService.removeByGoodsId(goods.getId());
            goodsPicService.addPatch(goods.getPics());
        }
        return code;
    }


    @Override
    public Goods getById(Integer id) throws Exception {
        Goods goods= goodsMapper.selectById(id);
        if(goods==null){
            throw new Exception("ID不存在");
        }
        return goodsMapper.selectById(id);
    }

    @Override
    public List<Goods> search(Goods goods) {

        return goodsMapper.select(goods);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Goods goods) {
        //分三步
        //1.启动分页查询
        PageHelper.startPage(page, limit);

        //2.调用Mapper层
        List<Goods> list = goodsMapper.select(goods);

        //3.构造PageInfo
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
